home *** CD-ROM | disk | FTP | other *** search
/ Delphi 2 - Developers' Solutions / Delphi 2 Developers' Solutions.iso / dds / chap01 / howto02 / delphi10 / ccwp.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-08-20  |  16.5 KB  |  436 lines

  1. unit Ccwp;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, StdCtrls, ExtCtrls, Buttons, Spin, FileCtrl;
  8.  
  9. type
  10.   TCCPaperBoySetupDialog = class(TForm)
  11.     Label1: TLabel;
  12.     BitBtn1: TBitBtn;
  13.     BitBtn2: TBitBtn;
  14.     Label2: TLabel;
  15.     Button1: TButton;
  16.     Edit1: TEdit;
  17.     Label3: TLabel;
  18.     OpenDialog1: TOpenDialog;
  19.     CheckBox1: TCheckBox;
  20.     SpinEdit1: TSpinEdit;
  21.     Label4: TLabel;
  22.     RadioGroup1: TRadioGroup;
  23.     BitBtn3: TBitBtn;
  24.     Timer1: TTimer;
  25.     FileListBox1: TFileListBox;
  26.     Image1: TImage;
  27.     procedure BitBtn2Click(Sender: TObject);
  28.     procedure BitBtn3Click(Sender: TObject);
  29.     procedure BitBtn1Click(Sender: TObject);
  30.     procedure RadioGroup1Click(Sender: TObject);
  31.     procedure CheckBox1Click(Sender: TObject);
  32.     procedure SpinEdit1Exit(Sender: TObject);
  33.     procedure Edit1Exit(Sender: TObject);
  34.     procedure Button1Click(Sender: TObject);
  35.     procedure Timer1Timer(Sender: TObject);
  36.     procedure FormCreate(Sender: TObject);
  37.     procedure FileListBox1Click(Sender: TObject);
  38.     procedure FormDestroy(Sender: TObject);
  39.   private
  40.     { Private declarations }
  41.   public
  42.     { Public declarations }
  43.     TheWorkingDirectory : String;           { This holds image file dir            }
  44.     TimeTillNextDelivery : Integer;         { This holds change interval in mins   }
  45.     TileTheWallpaper : Integer;             { This is the tile setting for windows }
  46.     TheWallpaperFilename : String;          { This is the full pathname for win    }
  47.     TimeLeftToChange : Integer;             { This is the counter for change time  }
  48.     SelectionOption : Integer;              { 1 = random; 2 = in order selection   }
  49.     CurrentSelection : Integer;             { This holds the position in the files }
  50.     procedure ReadTheWinIniFile;            { This gets the WIN.INI file data      }
  51.     procedure WriteTheWinIniFile;           { This writes the WIN.INI file data    }
  52.     procedure ReadThePaperboyIniFile;       { This gets the PAPERBOY INI data      }
  53.     procedure WriteThePaperboyIniFile;      { This writes the PAPERBOY INI data    }
  54.     procedure GetImageFilesInDirectory;     { This does ff/fn/fc to get files in d }
  55.     procedure UpdateTheImageDisplay;        { This puts an image in the TImage ctl }
  56.   end;
  57.  
  58. var
  59.   CCPaperBoySetupDialog : TCCPaperBoySetupDialog;
  60.   TheStringList         : TStringList;            { Used to get around FilLB Bug }
  61.  
  62. implementation
  63.  
  64. {$R *.DFM}
  65. uses
  66.   IniFiles ,  { Ini file handler unit       }
  67.   CCErrors;   { Generic error handling unit }
  68.  
  69. const
  70.  
  71.   PaperBoyIniFileName = 'CCPAPERB.INI';  { Make this a constant to save stack space }
  72.  
  73. { This updates the combobox when a new directory is chosen or at startup }
  74. procedure TCCPaperBoySetupDialog.GetImageFilesInDirectory;
  75. var Counter_1 : Integer;
  76. begin
  77.   { Clear the current selection }
  78.   CurrentSelection := -1;
  79.   { Put the working directory into the filelistbox }
  80.   FileListBox1.Directory := TheWorkingDirectory;
  81.   { Get an updated listing }
  82.   FileListBox1.Update;
  83.   { Move the strings over to the string list to avoid a bug }
  84.   TheStringList.Assign( FileListbox1.Items );
  85.   { Clear the itemindex to ensure a consistent state }
  86.   FileListBox1.ItemIndex := -1;
  87.   { Run through all the items looking for a match }
  88.   for Counter_1 := 1 to TheStringList.Count do
  89.   begin
  90.     { If match found set current selection and item index }
  91.     if TheStringList.strings[ Counter_1 - 1 ] = TheWallpaperFileName then
  92.     begin
  93.       CurrentSelection := Counter_1;
  94.       FileListBox1.ItemIndex := Counter_1 - 1;
  95.       break;
  96.     end;
  97.   end;
  98.   { If no match found check for any bitmap files present }
  99.   if FileListBox1.ItemIndex = -1 then
  100.   begin
  101.     { If at least one bitmap present make it the default selction }
  102.     if FileListBox1.Items.Count > 0 then
  103.     begin
  104.       TheWallPaperFileName := FileListBox1.Items.Strings[ 0 ];
  105.       CurrentSelection := 0;
  106.       FileListbox1.ItemIndex := 0;
  107.     end
  108.     else
  109.     begin
  110.       { Otherwise have no selection }
  111.       TheWallpaperFileName := '';
  112.       CurrentSelection := -1;
  113.     end;
  114.   end;
  115. end;
  116.  
  117. { This encapsulates putting a new image in the display; it will eventually }
  118. { be used to store a GIF as a bitmap for windows convenience.              }
  119. procedure TCCPaperBoySetupDialog.UpdateTheImageDisplay;
  120. begin
  121.   { avoid automatic error if no valid filename }
  122.   if TheWallpaperfileName = '' then exit;
  123.   {$I+}
  124.   try   { Attempt to load the bitmap file }
  125.     Image1.Picture.LoadFromFile( TheWorkingdirectory + TheWallpaperfilename );
  126.   except
  127.     on E : EInOutError do { If a file error signal it }
  128.     begin
  129.       ErrorDialog( 'Cannot Load ' + TheWallpaperfilename + ' due to '
  130.        + GetIOErrorMessage( E.ErrorCode ));
  131.     end;
  132.     on E : EInvalidGraphic do { If a conversion error signal it }
  133.     begin
  134.       ErrorDialog( 'Cannot Load ' + TheWallpaperFilename + ' due to ' + E.message );
  135.     end;
  136.   end;
  137. end;
  138.  
  139. { This procedure reads in the configuration from the WIN INI file }
  140. procedure TCCPaperBoySetupDialog.ReadTheWinIniFile;
  141. var
  142.    { The excellent TIniFile object handles the complexity of Windows INI files! }
  143.    TheIniFile : TIniFile;
  144. begin
  145.   { Combine assignfile and reset into one call with create }
  146.   TheIniFile := TIniFile.Create( 'WIN.INI' );
  147.   { Use a try block to make sure the ini file is closed by free call }
  148.   try
  149.     { Use the Tinifile object's read methods to get the configuration data }
  150.     with TheIniFile do
  151.     begin
  152.       { Use readinteger to get whether to tile the wallpaper }
  153.       TileTheWallpaper := ReadInteger( 'Desktop', 'TileWallpaper', 0 );
  154.       { Use readstring to get the wallpaper filename }
  155.       TheWallpaperFilename := ExtractFileName( ReadString( 'Desktop', 'wallpaper', '' ));
  156.       { Get windows working wallpaper directory in case Paperboy not inited }
  157.       TheWorkingDirectory := ExtractFilePath( ReadString( 'Desktop', 'wallpaper' , '' ));
  158.     end;
  159.   finally
  160.     { Regardless of success or failure, free the TIniFile object }
  161.     TheIniFile.Free;
  162.   end;
  163. end;
  164.  
  165. { This procedure writes out the configuration to the WIN INI file }
  166. procedure TCCPaperBoySetupDialog.WriteTheWinIniFile;
  167. var
  168.    { Again use the very useful TIniFile object to encapsulate Windows INI files }
  169.    TheIniFile : TIniFile;
  170.    ThePChar : PChar;
  171. begin
  172.   { Combine an AssignFile and Reset call into the create method }
  173.   TheIniFile := TIniFile.Create( 'WIN.INI' );
  174.   try
  175.     { Use the TIniFile object's methods to write out the data }
  176.     with TheIniFile do
  177.     begin
  178.       { Use writeinteger to send wether to tile the wallpaper }
  179.       WriteInteger( 'Desktop' , 'TileWallpaper' , TileTheWallpaper );
  180.       { Use writestring to send the image file name }
  181.       WriteString( 'Desktop' , 'wallpaper' , TheWorkingDirectory + TheWallpaperFileName );
  182.     end;
  183.   finally
  184.     { Regardless of above calls, free the TIniFile object }
  185.     TheIniFile.Free;
  186.   end;
  187.   { Obtain memory for the windows string }
  188.   GetMem( ThePChar , 255 );
  189.   { Copy the complete pathname into it }
  190.   StrPCopy( ThePChar , TheWorkingDirectory + TheWallpaperFileName );
  191.   { Send the request to windows via arcane API call }
  192.   SystemParametersInfo( SPI_SETDESKWALLPAPER , 0 , ThePChar , SPIF_SENDWININICHANGE );
  193.   { And let the memory go }
  194.   Freemem( ThePChar , 255 );
  195. end;
  196.  
  197. { This procedure reads in the configuration from an INI file }
  198. procedure TCCPaperBoySetupDialog.ReadThePaperBoyIniFile;
  199. var
  200.    { The excellent TIniFile object handles the complexity of Windows INI files! }
  201.    TheIniFile : TIniFile;
  202. begin
  203.   { Combine assignfile and reset into one call with create }
  204.   TheIniFile := TIniFile.Create( PaperBoyIniFileName );
  205.   { Use a try block to make sure the ini file is closed by free call }
  206.   try
  207.     { Use the Tinifile object's read methods to get the configuration data }
  208.     with TheIniFile do
  209.     begin
  210.       { Use readinteger to get the interval till the next wallpaper change in min }
  211.       TimeTillNextDelivery := ReadInteger( 'Setup', 'TimeToNextChange', 10 );
  212.       { Use readinteger to get whether to do random or in order image selection }
  213.       SelectionOption := ReadInteger( 'Setup', 'SelectionOption', 1 );
  214.       { Use readstring to get the directory to look for image files }
  215.       { ( Pass in default from WIN.INI in case Paperboy not inited) }
  216.       TheWorkingDirectory := ReadString( 'Setup', 'WorkingDirectory', TheWorkingDirectory );
  217.     end;
  218.   finally
  219.     { Regardless of success or failure, free the TIniFile object }
  220.     TheIniFile.Free;
  221.   end;
  222. end;
  223.  
  224. { This procedure writes out the configuration to an INI file }
  225. procedure TCCPaperBoySetupDialog.WriteThePaperBoyIniFile;
  226. var
  227.    { Again use the very useful TIniFile object to encapsulate Windows INI files }
  228.    TheIniFile : TIniFile;
  229. begin
  230.   { Combine an AssignFile and Reset call into the create method }
  231.   TheIniFile := TIniFile.Create( PaperBoyIniFileName );
  232.   try
  233.     { Use the TIniFile object's methods to write out the data }
  234.     with TheIniFile do
  235.     begin
  236.       { Use writeinteger to send the time interval between changes in mins }
  237.       WriteInteger( 'Setup' , 'TimeToNextChange' , TimeTillNextDelivery );
  238.       { Use writeinteger to send whether to use random or in order selection }
  239.       WriteInteger( 'Setup' , 'SelectionOption' , SelectionOption );
  240.       { Use writestring to send the Working directory for image files }
  241.       WriteString( 'Setup' , 'WorkingDirectory' , TheWorkingDirectory );
  242.     end;
  243.   finally
  244.     { Regardless of above calls, free the TIniFile object }
  245.     TheIniFile.Free;
  246.   end;
  247. end;
  248.  
  249. { Delphi wrote this; it assumes cancel, so no data sent to windows just hide }
  250. procedure TCCPaperBoySetupDialog.BitBtn2Click(Sender: TObject);
  251. begin
  252.   { Hide the setup dialog without saving changes }
  253.   WindowState := wsMinimized;
  254. end;
  255.  
  256. { Delphi wrote this; it exits the entire program without saving }
  257. procedure TCCPaperBoySetupDialog.BitBtn3Click(Sender: TObject);
  258. begin
  259.   { Don't write the changes; assume just leaving }
  260.   Close;
  261. end;
  262.  
  263. { Delphi wrote this; it assumes save data and minimize is the action }
  264. procedure TCCPaperBoySetupDialog.BitBtn1Click(Sender: TObject);
  265. begin
  266.   { Write the changes to the WIN INI file }
  267.   WriteTheWinIniFile;
  268.   { Write the changes to the PAPERBOY INI file }
  269.   WriteThePaperBoyIniFile;
  270.   { Hide the setup dialog after saving the changes }
  271.   WindowState := wsMinimized;
  272. end;
  273.  
  274. { Delphi wrote this; it sets the internal variable to the radiogroup state }
  275. procedure TCCPaperBoySetupDialog.RadioGroup1Click(Sender: TObject);
  276. begin
  277.   { Set the selection option based on the selected radiogroup item }
  278.   SelectionOption := RadioGroup1.ItemIndex + 1;
  279. end;
  280.  
  281. { Delphi wrote this; it sets the internal variable to the checkbox state }
  282. procedure TCCPaperBoySetupDialog.CheckBox1Click(Sender: TObject);
  283. begin
  284.   { Set the tiling based on whether the box is checked }
  285.   if CheckBox1.Checked then TileTheWallpaper := 1 else
  286.    TileTheWallpaper := 0;
  287. end;
  288.  
  289. { Delphi wrote this; it sets the data from the spin edit into the timer var }
  290. procedure TCCPaperBoySetupDialog.SpinEdit1Exit(Sender: TObject);
  291. begin
  292.   { If new data entered grab it and make a change }
  293.   if SpinEdit1.Modified then
  294.   begin
  295.     { Reset the delivery interval from the spinedit control }
  296.     TimeTillNextDelivery := StrToInt( SpinEdit1.Text );
  297.   end;
  298. end;
  299.  
  300. { Delphi wrote this; it checks on exit of the edit control for a change }
  301. { and updates the system if one was made.                               }
  302. procedure TCCPaperBoySetupDialog.Edit1Exit(Sender: TObject);
  303. begin
  304.   { If something has been entered grab it }
  305.   if Edit1.Modified then
  306.   begin
  307.     { provided the chosen wallpaper exists get it and its dir }
  308.     if FileExists( Edit1.Text ) then
  309.     begin
  310.       { Get the new working directory }
  311.       TheWorkingDirectory := ExtractFilePath( Edit1.Text );
  312.       { Extract the filename for sending to windows }
  313.       TheWallPaperFilename := ExtractFileName( Edit1.Text );
  314.       { Get the list of image files and put in combobox }
  315.       GetImageFilesInDirectory;
  316.       { Put in a preview just for fun }
  317.       UpdateTheImageDisplay;
  318.     end;
  319.   end;
  320. end;
  321.  
  322. { Delphi wrote this; it runs an OpenDialog and then updates from the result }
  323. procedure TCCPaperBoySetupDialog.Button1Click(Sender: TObject);
  324. begin
  325.   { If the open dialog isn't cancelled then get the info }
  326.   if OpenDialog1.Execute then
  327.   begin
  328.     Edit1.Text := OpenDialog1.FileName;
  329.     { provided the chosen wallpaper exists get it and its dir }
  330.     if FileExists( Edit1.Text ) then
  331.     begin
  332.       { Get the new working directory }
  333.       TheWorkingDirectory := ExtractFilePath( Edit1.Text );
  334.       { Extract the filename for sending to windows }
  335.       TheWallPaperFilename := ExtractFileName( Edit1.Text );
  336.       { Get the list of image files and put in combobox }
  337.       GetImageFilesInDirectory;
  338.       { Put in a preview just for fun }
  339.       UpdateTheImageDisplay;
  340.     end;
  341.   end;
  342. end;
  343.  
  344. { Delphi wrote this; it is the actual routine to update the wallpaper }
  345. procedure TCCPaperBoySetupDialog.Timer1Timer(Sender: TObject);
  346. begin
  347.   { abort if no valid selection }
  348.   if CurrentSelection = -1 then exit;
  349.   { Decrease the available time by 1 minute }
  350.   TimeLeftToChange := TimeLeftToChange - 1;
  351.   { If the time has run out, do a wallpaper change }
  352.   if TimeLeftToChange = 0 then
  353.   begin
  354.     { Do either a random or an inorder change }
  355.     case SelectionOption of
  356.       1 : begin { Select a new wallpaper randomly }
  357.             if FileListbox1.ItemIndex <> -1 then { If there are no items don't change }
  358.             begin
  359.               { Get a random number equal to 1 to total items in list }
  360.               CurrentSelection := Random( TheStringList.Count ) + 1;
  361.               { Get the filename from the combobox list }
  362.               TheWallPaperFileName := TheStringList.Strings[ CurrentSelection - 1 ];
  363.               { Send the info out to windows }
  364.               WriteTheWinIniFile;
  365.               { And reset the counter back to start for next loop }
  366.               TimeLeftToChange := TimeTillNextDelivery;
  367.             end;
  368.           end;
  369.       2 : begin { Move down 1 and then loop back to top }
  370.             { If there are no items listed then exit }
  371.             if FileListbox1.Items.Count > 0 then
  372.             begin
  373.               { Otherwise bump the selection up one }
  374.               CurrentSelection := CurrentSelection + 1;
  375.               { If past the maximum entries then loop back to 1 }
  376.               if CurrentSelection > TheStringList.Count then
  377.                CurrentSelection := 1;
  378.               { Get the filename from the combobox }
  379.               TheWallPaperFileName := TheStringList.Strings[ CurrentSelection - 1 ];
  380.               { Tell windows about the changes }
  381.               WriteTheWinIniFile;
  382.               { And reset the counter for next time }
  383.               TimeLeftToChange := TimeTillNextDelivery;
  384.             end;
  385.           end;
  386.     end;
  387.   end;
  388. end;
  389.  
  390. { Delphi wrote this; it is used to create all the data on startup }
  391. procedure TCCPaperBoySetupDialog.FormCreate(Sender: TObject);
  392. begin
  393.   { Seed the random number generator }
  394.   Randomize;
  395.   { Create the string list }
  396.   TheStringList := TStringList.Create;
  397.   { Get the windows data }
  398.   ReadTheWinIniFile;
  399.   { Get the paperboy data }
  400.   ReadThePaperboyIniFile;
  401.   { Set the checkbox for tiled wallpaper }
  402.   CheckBox1.Checked := ( TileTheWallpaper = 1 );
  403.   { Set the radiogroup to the option selected }
  404.   RadioGroup1.ItemIndex := SelectionOption - 1;
  405.   { Set the SpinEdit to the read in time value }
  406.   SpinEdit1.Text := IntToStr( TimeTillNextDelivery );
  407.   { Set the Working Directory to the imported value }
  408.   Edit1.Text := TheWorkingDirectory;
  409.   { Clear the time left timer }
  410.   TimeLeftToChange := TimeTillNextDelivery;
  411.   { Get the list of image files and put in combobox }
  412.   GetImageFilesInDirectory;
  413.   { Put in a preview just for fun }
  414.   UpdateTheImageDisplay;
  415. end;
  416.  
  417. { Delphi wrote this; it sets the new wallpaper filename to the selection }
  418. { and updates the preview display                                        }
  419. procedure TCCPaperBoySetupDialog.FileListBox1Click(Sender: TObject);
  420. begin
  421.   { If there is something selected in the FLB make it new filename }
  422.   if FileListbox1.ItemIndex <> -1 then
  423.    TheWallPaperFileName := FileListbox1.Items.Strings[ FileListbox1.ItemIndex ];
  424.   { Update the preview display }
  425.   UpdateTheImageDisplay;
  426. end;
  427.  
  428. { Delphi wrote this; put in code to free the stringlist to save memory! }
  429. procedure TCCPaperBoySetupDialog.FormDestroy(Sender: TObject);
  430. begin
  431.   { Free the string list }
  432.   TheStringList.Free;
  433. end;
  434.  
  435. end.
  436.